package org.drawlineapi.tools {
	import flash.display.Sprite;
	import flash.geom.Point;
	import org.drawlineapi.tools.icons.Arrow;
	import org.drawlineapi.tools.SimpleLine;
	
	/**
	 * ...
	 * @author Ricardo Teixeira
	 */
	public class IndicativeLine extends AbsLine {
		
		private var _startIcon:Sprite = new Sprite ();
		private var _endIcon:Sprite = new Sprite ();
		
		public function IndicativeLine(from:Point, to:Point) {
			super(from, to);
			
			this._startIcon = new Arrow ();
			this._endIcon = new Arrow ();
			
			Arrow (this._startIcon).format = this.format;
			Arrow (this._endIcon).format = this.format;
			
			this.addChild (this._startIcon );
			this.addChild (this._endIcon );
			
			this.mouseEnabled = false;
		}
		
		public function enableStartIcon ():void {
			this._startIcon.visible = true;
		}
		
		public function desableStartIcon ():void {
			this._startIcon.visible = false;
		}
		
		public function enableEndIcon ():void {
			this._endIcon.visible = true;
		}
		
		public function desableEndIcon ():void {
			this._endIcon.visible = false;
		}
		
		override public function draw(from:Point, to:Point):void {
			super.draw(from, to);
		}
		
		override protected function update():void {
			super.update();
			
			if (this.to != null) {
				this._endIcon.x = this.to.x;
				this._endIcon.y = this.to.y;
				
				var rad:Number = Math.atan2 (this.to.y - this.from.y, this.to.x - this.from.x);
				var ang:Number = 180 / Math.PI * rad;
				
				
				this._startIcon.x = this.from.x;
				this._startIcon.y = this.from.y;
				
				this._startIcon.rotation = ang + 180;
				this._endIcon.rotation = ang;
				
			}
		}
		
		public function changeStartIcon(value:Sprite):void {
			if (this.contains (this._startIcon) ) {
				this.removeChild (this._startIcon );
			}
			_startIcon = value;
			this.addChild (this._startIcon );
			this.update ();
		}
		
		public function changeEndIcon(value:Sprite):void {
			if (this.contains (this._endIcon) ) {
				this.removeChild (this._endIcon );
			}
			_endIcon = value;
			this.addChild (this._endIcon );
			this.update ();
		}
		
	}

}